package com.ztf0337.mycurriculumdesign.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textview.MaterialTextView;
import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.TimeConverters;
import com.ztf0337.mycurriculumdesign.Utils.TransitionUtils;
import com.ztf0337.mycurriculumdesign.entity.RentedParkingSpace;
import com.ztf0337.mycurriculumdesign.service.RentService;

import java.util.Locale;

public class MyRentDetailActivity extends AppCompatActivity {
    private TextView tvCountdown,tvPSNumber,tvParkLotName,tvLicencePlate,tvType,tvLocation,tvStartTime;
    private Button btnAction;
    private Runnable timerRunnable;
    private ImageView btnBack;
    private Handler handler = new Handler();
    private RentService rentService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_rent_detail);
        rentService = new RentService(this);


        Intent intent = getIntent();
        Long id = intent.getLongExtra("id",-1L);
        boolean rent = intent.getBooleanExtra("rent",false);

        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> {
            if (rent) {
                Intent intent1 = new Intent(this, MainActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent1.putExtra("open",1);
                startActivity(intent1);
            }
            finish();
        });

        if (id.equals(-1L)) {
            Toast.makeText(this, "系统错误", Toast.LENGTH_SHORT).show();
            finish();
        }

        RentedParkingSpace rentedParkingSpace = rentService.getRentById(id);
        tvPSNumber = findViewById(R.id.tvPSNumber);
        tvPSNumber.setText(rentedParkingSpace.getParkingSpaceNumber());
        tvParkLotName = findViewById(R.id.tvParkLotName);
        tvParkLotName.setText(rentedParkingSpace.getParkLotName());
        tvLicencePlate = findViewById(R.id.tvLicencePlate);
        tvLicencePlate.setText(rentedParkingSpace.getLicencePlate());
        tvType = findViewById(R.id.tvType);
        tvType.setText(TransitionUtils.transParkType(rentedParkingSpace.getType()));
        tvLocation = findViewById(R.id.tvLocation);
        tvLocation.setText(rentedParkingSpace.getLocation());
        tvStartTime = findViewById(R.id.tvStartTime);
        tvStartTime.setText(TimeConverters.format(rentedParkingSpace.getStartTime()));

        btnAction = findViewById(R.id.btnAction);
        btnAction.setOnClickListener(v ->{
            Log.d("测试","结束租借订单发送id "+rentedParkingSpace.getOrderId());
            Intent intent1 = new Intent(this, SettleAccountsActivity.class);
            intent1.putExtra("id",rentedParkingSpace.getId());
            this.startActivity(intent1);
        });

        long targetTimeMillis = rentedParkingSpace.getStartTime();
        tvCountdown = findViewById(R.id.tvCountdown);
        timerRunnable = new Runnable() {
            @Override
            public void run() {
                long now = System.currentTimeMillis();
                long diff = now - targetTimeMillis;
                if (diff > 0) {
                    // 计算小时、分钟、秒
                    long seconds = diff / 1000 % 60;
                    long minutes = diff / (1000 * 60) % 60;
                    long hours = diff / (1000 * 60 * 60);
                    // 格式化为 HH:mm:ss
                    String timeStr = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
                    tvCountdown.setText(timeStr);
                    handler.postDelayed(this, 1000);
                } else {
                    tvCountdown.setText("00:00:00");
                    tvCountdown.setTextColor(Color.RED);
                    handler.removeCallbacks(timerRunnable);
                }
            }
        };
        handler.post(timerRunnable); // 启动计时
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(timerRunnable); // 防止内存泄漏
    }

}
